#include "stdafx.h"

#include "texturemanager.h"

TextureManager::TextureManager( const int textures )
{
	mnCurrentTextures = 0;
    mnTextures = textures;

    mGLImageNames = new GLuint[textures];

    ilutRenderer( ILUT_OPENGL );

    glGenTextures( textures, &mGLImageNames[0] );
}

TextureManager::~TextureManager( void )
{
    delete[] mGLImageNames;
}

GLuint TextureManager::GetTexture( const int rId )
{
	return mGLImageNames[rId];
}

int TextureManager::LoadImage( const char* filename )
{
    ILuint imageId;									
	ilGenImages( 1, &imageId );								
	ilBindImage( imageId );	

	if( !ilLoadImage( filename ) )  					
	{	
		ilDeleteImages( 1, &imageId );	

		std::cerr << " Error loading texture: " << filename << std::endl;

        return -1;
	}

	mGLImageNames[mnCurrentTextures++] = ilutGLBindTexImage(); 

	ILenum error;

	while( (error = ilGetError()) != IL_NO_ERROR ) 
	{
        std::cerr << "Texture error: " << error << ": " << iluErrorString( error ) << std::endl;
	}

   	ilDeleteImages( 1, &imageId );

    return mnCurrentTextures;
}